package Day2025_03_05;

import java.util.Stack;

public class LeetCode227 {
    public int calculate(String s) {
        int n = s.length();
        Stack<Integer> stack = new Stack<>();
        char[] ch = s.toCharArray();
        int i = 0;
        char op = '+';
        while (i < n) {
            if (ch[i] == ' ') i++;
            else if (ch[i] >= '0' && ch[i] <= '9') {
                int tmp = 0;
                while (i < n && ch[i] >= '0' && ch[i] <= '9') {
                    tmp = tmp * 10 + (ch[i] - '0');
                    i++;
                }
                if (op == '+') stack.push(tmp);
                else if (op == '-') stack.push(-tmp);
                else if (op == '*') stack.push(stack.pop() * tmp);
                else stack.push(stack.pop() / tmp);
            } else {
                op = ch[i];
                i++;
            }
        }
        int tmp = 0;
        while (!stack.isEmpty()) tmp += stack.pop();
        return tmp;
    }

    public static void main(String[] args) {
        LeetCode227 leetCode227 = new LeetCode227();
        System.out.println(leetCode227.calculate("42"));
    }
}
